package com.aco.fatureapp.printer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

public class MobilePrinter {
	private BTOperator bto;
	private BTPrinter btp;
	public static final int AT_LEFT = 0;
	public static final int AT_CENTER = 1;
	public static final int AT_RIGHT = 2;
	public static final int BC_UPCA = 65;
	public static final int BC_UPCB = 66;
	public static final int BC_EAN13 = 67;
	public static final int BC_EAN8 = 68;
	public static final int BC_CODE39 = 69;
	public static final int BC_ITF = 70;
	public static final int BC_CODEBAR = 71;
	public static final int BC_CODE93 = 72;
	public static final int BC_CODE128 = 73;
	public static final int BC_DEFAULT = 0;
	public static final int BC_HRINONE = 0;
	public static final int BC_HRIUNDER = 1;
	public static final int BC_HRIBELOW = 2;
	public static final String TC_GB2312 = "gb2312";
	public static final String TC_UTF8 = "utf-8";
	public static final int PS_NORMAL = 0;
	public static final int PS_PRAPEROUT = 3;
	public static final int PS_ERR = 5;
	private int iStatusCode;
	private int iPrintCode;

	private static final UUID MY_UUID = UUID
			.fromString("00001101-0000-1000-8000-00805F9B34FB");

	public MobilePrinter() {
		this.iStatusCode = 0;
		this.iPrintCode = 0;
	}

	public int getCurrentStatus() {
		return this.iPrintCode;
	}

	public boolean lnitLib() {
		Log.d("REGOLIB", "LnitLib...");

		boolean bStart = false;
		if (this.iStatusCode == 0) {
			this.bto = new BTOperator();
			this.btp = new BTPrinter();

			bStart = this.bto.BTO_EnableBluetooth();
			if (bStart) {
				Log.d("REGOLIB",
						"LnitLib --> Status OK, the bluetooth is started");

				this.iStatusCode = 1;
				try {
					Thread.sleep(1500L);
				}
				catch (InterruptedException e1) {
					e1.printStackTrace();
				}
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return bStart;
	}

	public ArrayList<String> getBondedDevices() {
		Log.d("REGOLIB", "GetBondedDevices...");

		ArrayList<String> btList = null;

		if (this.iStatusCode == 1) {
			Set<BluetoothDevice> pairedDevices = this.bto.BTO_GetBondedDevice();

			if (pairedDevices.size() > 0) {
				btList = new ArrayList<String>();
				for (BluetoothDevice device : pairedDevices) {
					if ("MPT-II".equals(device.getName())) {
						btList.add(device.getName());
						btList.add(device.getAddress());
					}
					else if ("MPT-III".equals(device.getName())) {
						btList.add(device.getName());
						btList.add(device.getAddress());
					}
				}
				this.iStatusCode = 2;
			}
			else {
				Log.d("REGOLIB", "GetBondedDevices --> no pairedDevices");
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return btList;
	}

	public boolean connectDevice(ArrayList<String> btList, int iIndex) {
		Log.d("REGOLIB", "ConnectDevice...");

		if (this.iStatusCode == 2) {
			String strMac = btList.get(iIndex + 1);

			Log.d("REGOLIB", "ConnectDevice --> ConnectDevice " + strMac);
			if (this.bto.BTO_ConnectDevice(strMac)) {
				Log.d("REGOLIB", "ConnectDevice --> ConnectDevice succeed");
				if (this.bto.BTO_GetIOInterface()) {
					Log.d("REGOLIB", "ConnectDevice --> GetIOInterface succeed");
					this.iStatusCode = 3;
					return true;
				}
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return false;
	}

	public boolean closeConnect() {
		Log.d("REGOLIB", "CloseConnect...");

		boolean bRet = false;
		if (3 == this.iStatusCode) {
			bRet = this.bto.BTO_CloseDevice();
			if (bRet) {
				Log.d("REGOLIB", "CloseConnect succeed");
				this.iStatusCode = 1;
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return bRet;
	}

	public boolean freeLib(boolean bDisable) {
		Log.d("REGOLIB", "FreeLib...");

		if (bDisable) {
			if (this.bto.BTO_DisableBluetooth()) {
				Log.d("REGOLIB", "FreeLib --> BTO_DisableBluetooth succeed");
				this.iStatusCode = 0;
				return true;
			}
		}
		return false;
	}

	public boolean sendString(String str) throws UnsupportedEncodingException {
		Log.d("REGOLIB", "SendString...");

		if (3 == this.iStatusCode) {
			byte[] msg = str.getBytes("gb2312");

			if (this.bto.BTO_Write(msg, msg.length)) {
				return true;
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return false;
	}

	public boolean sendBuffer(byte[] data, int iLeng) {
		Log.d("REGOLIB", "SendBuffer...");

		if (3 == this.iStatusCode) {
			if (this.bto.BTO_Write(data, iLeng)) {
				return true;
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return false;
	}

	public boolean reset() {
		Log.d("REGOLIB", "Reset...");

		if (3 == this.iStatusCode) {
			byte[] bLen = new byte[1];
			byte[] data = this.btp.MakeComm(0, 0, 0, bLen);
			if (this.bto.BTO_Write(data, bLen[0])) {
				return true;
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return false;
	}

	public boolean feedLines(int iNum) {
		Log.d("REGOLIB", "FeedLines...");

		if (3 == this.iStatusCode) {
			byte[] bLen = new byte[1];
			byte[] data = this.btp.MakeComm(1, iNum, 0, bLen);
			if (this.bto.BTO_Write(data, bLen[0])) {
				return true;
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return false;
	}

	public boolean setPrintPosition(int iLeftMargin, int iWidth) {
		Log.d("REGOLIB", "SetPrintPosition...");

		if (3 == this.iStatusCode) {
			byte[] bLen1 = new byte[1];
			byte[] data1 = this.btp.MakeComm(2, iLeftMargin, 0, bLen1);
			if (!this.bto.BTO_Write(data1, bLen1[0])) {
				return false;
			}
			byte[] bLen2 = new byte[1];
			byte[] data2 = this.btp.MakeComm(3, iWidth, 0, bLen2);
			if (this.bto.BTO_Write(data2, bLen2[0])) {
				return true;
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return false;
	}

	public boolean oppositeColor(boolean bOpposite) {
		Log.d("REGOLIB", "OppositeColor...");

		if (3 == this.iStatusCode) {
			byte[] bLen = new byte[1];
			int iVal = 0;
			if (bOpposite) {
				iVal = 1;
			}
			byte[] data = this.btp.MakeComm(4, iVal, 0, bLen);
			if (this.bto.BTO_Write(data, bLen[0])) {
				return true;
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return false;
	}

	public boolean setLineSpace(int iLines) {
		Log.d("REGOLIB", "SetLineSpace...");

		if (3 == this.iStatusCode) {
			byte[] bLen = new byte[1];
			if (-1 == iLines) {
				byte[] data = this.btp.MakeComm(5, 0, 1, bLen);
				if (this.bto.BTO_Write(data, bLen[0])) {
					return true;
				}
			}
			else {
				byte[] data = this.btp.MakeComm(5, iLines, 0, bLen);
				if (this.bto.BTO_Write(data, bLen[0])) {
					return true;
				}
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return false;
	}

	public boolean alignType(int alignType) {
		Log.d("REGOLIB", "AlignType...");

		if (3 == this.iStatusCode) {
			byte[] bLen = new byte[1];
			byte[] data = this.btp.MakeComm(6, alignType, 0, bLen);
			if (this.bto.BTO_Write(data, bLen[0])) {
				return true;
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return false;
	}

	public boolean formatString(boolean width, boolean height, boolean bold,
			boolean underline, boolean minifont) {
		Log.d("REGOLIB", "FormatString...");

		if (3 == this.iStatusCode) {
			if ((!width) && (!height) && (!bold) && (!underline) && (!minifont)) {
				return true;
			}
			byte[] bLen = new byte[1];
			int datacom = this.btp.MakeFormat(width, height, bold, underline,
					minifont);
			byte[] data = this.btp.MakeComm(13, datacom, 0, bLen);
			if (this.bto.BTO_Write(data, bLen[0])) {
				return true;
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return false;
	}

	public boolean duplePrint(boolean bdup) {
		Log.d("REGOLIB", "DuplePrint...");

		if (3 == this.iStatusCode) {
			byte[] bLen = new byte[1];
			int iVal = 0;
			if (bdup) {
				iVal = 1;
			}
			byte[] data = this.btp.MakeComm(7, iVal, 0, bLen);
			if (this.bto.BTO_Write(data, bLen[0])) {
				return true;
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return false;
	}

	public boolean printBarcode(int iType, int iWidth, int iHeight, int hri,
			String strData) {
		Log.d("REGOLIB", "PrintBarcode...");

		if (3 == this.iStatusCode) {
			if ((iWidth <= 0) || (iWidth > 4)) {
				iWidth = 2;
			}
			byte[] bLen1 = new byte[1];
			byte[] data1 = this.btp.MakeComm(10, iWidth, 0, bLen1);
			if (!this.bto.BTO_Write(data1, bLen1[0])) {
				return false;
			}
			System.out.println("BTP_barWidth " + data1.length);
			for (int i = 0; i < 3; i++) {
				int hex = data1[i] & 0xFF;
				System.out.print(Integer.toHexString(hex) + " ");
			}
			System.out.println();

			if (iHeight <= 0) {
				iHeight = 36;
			}
			byte[] bLen2 = new byte[1];
			byte[] data2 = this.btp.MakeComm(11, iHeight, 0, bLen2);
			if (!this.bto.BTO_Write(data2, bLen2[0])) {
				return false;
			}
			System.out.println("BTP_barHeight " + data2.length);
			for (int i = 0; i < 3; i++) {
				int hex = data2[i] & 0xFF;
				System.out.print(Integer.toHexString(hex) + " ");
			}
			System.out.println();

			if ((hri < 0) || (hri > 2)) {
				hri = 2;
			}
			byte[] bLen3 = new byte[1];
			byte[] data3 = this.btp.MakeComm(12, hri, 0, bLen3);
			if (!this.bto.BTO_Write(data3, bLen3[0])) {
				return false;
			}
			System.out.println("BTP_barHRI " + data3.length);
			for (int i = 0; i < 3; i++) {
				int hex = data3[i] & 0xFF;
				System.out.print(Integer.toHexString(hex) + " ");
			}
			System.out.println();

			byte[] bLen4 = new byte[1];
			byte[] data4 = this.btp.MakeComm(8, iType, strData.length(), bLen4);
			if (!this.bto.BTO_Write(data4, bLen4[0])) {
				return false;
			}
			System.out.println("BTP_barCode " + data4.length);
			for (int i = 0; i < 3; i++) {
				int hex = data4[i] & 0xFF;
				System.out.print(Integer.toHexString(hex) + " ");
			}
			System.out.println();

			byte[] bLen5 = strData.getBytes();
			if (!this.bto.BTO_Write(bLen5, bLen5.length)) {
				return false;
			}
			System.out.println("BTP_barSTR " + bLen5.length);
			for (int i = 0; i < bLen5.length; i++) {
				int hex = bLen5[i] & 0xFF;
				System.out.print(Integer.toHexString(hex) + " ");
			}
			System.out.println();

			return true;
		}

		this.iPrintCode = 5;

		return false;
	}

	public int queryPrinterStatus() {
		Log.d("REGOLIB", "QueryPrinterStatus...");

		byte[] buffer = new byte[1];
		int iPrintStatus = 5;
		if (3 == this.iStatusCode) {
			byte[] bLen = new byte[1];
			byte[] data = this.btp.MakeComm(9, 0, 0, bLen);
			if (this.bto.BTO_Write(data, bLen[0])) {
				if (this.bto.BTO_Read(buffer, buffer.length)) {
					switch (buffer[0]) {
					case 0:
						Log.d("REGOLIB", "QueryPrinterStatus --> PS_NORMAL");
						iPrintStatus = 0;
						break;
					case 1:
						Log.d("REGOLIB", "QueryPrinterStatus --> PS_PRAPEROUT");
						iPrintStatus = 3;
					default:
						break;
					}
				}
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return iPrintStatus;
	}

	public boolean printLocalFile(String filePath, String codeType) {
		Log.d("REGOLIB", "PrintLocalFile...");

		boolean bRet = true;
		if (3 == this.iStatusCode) {
			try {
				StringBuffer sBuffer = new StringBuffer();
				FileInputStream fInputStream = new FileInputStream(filePath);
				InputStreamReader inputStreamReader = new InputStreamReader(
						fInputStream, codeType);
				BufferedReader in = new BufferedReader(inputStreamReader);
				if (!new File(filePath).exists()) {
					Log.d("REGOLIB", "PrintLocalFile --> Local file is invalid");
					bRet = false;
				}
				else {
					while (in.ready()) {
						sBuffer.append(in.readLine() + "\n");
					}
					Log.d("REGOLIB", "PrintLocalFile --> Read file succeed");

					byte[] bSend = sBuffer.toString().getBytes("gb2312");
					this.bto.BTO_Write(bSend, bSend.length);
				}
				in.close();
			}
			catch (Exception e) {
				Log.d("REGOLIB", "PrintLocalFile --> Operate failed");
				e.printStackTrace();
				bRet = false;
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return bRet;
	}

	public String querySDKVersion() {
		Log.d("REGOLIB", "QuerySDKVersion...");

		return "1.0.1";
	}

	private class BTOperator {
		private BluetoothAdapter mBluetoothAdapter = null;
		private InputStream mmInStream;
		private OutputStream mmOutStream;
		private BluetoothSocket mmSocket;
		private BluetoothDevice mmDevice;

		public BTOperator() {
		}

		public boolean BTO_EnableBluetooth() {
			boolean bRet = false;
			this.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
			if (this.mBluetoothAdapter != null) {
				if (this.mBluetoothAdapter.isEnabled()) {
					return true;
				}
				this.mBluetoothAdapter.enable();

				if (!this.mBluetoothAdapter.isEnabled()) {
					bRet = true;
					MobilePrinter.this.iStatusCode = 1;
					Log.d("REGOLIB", "BTO_EnableBluetooth --> Open OK");
				}
			}
			else {
				Log.d("REGOLIB",
						"BTO_EnableBluetooth --> mBluetoothAdapter is null");
			}
			return bRet;
		}

		public boolean BTO_DisableBluetooth() {
			Log.d("REGOLIB", "BTO_DisableBluetooth...");

			boolean bRet = false;

			if (this.mBluetoothAdapter != null) {
				if (this.mBluetoothAdapter.isEnabled()) {
					this.mBluetoothAdapter.disable();
				}
				if (!this.mBluetoothAdapter.isEnabled()) {
					bRet = true;

					Log.d("REGOLIB", "BTO_DisableBluetooth --> Close OK");
				}
			}
			else {
				Log.d("REGOLIB",
						"BTO_DisableBluetooth --> mBluetoothAdapter is null");
			}
			return bRet;
		}

		public Set<BluetoothDevice> BTO_GetBondedDevice() {
			Log.d("REGOLIB", "BTO_GetBondedDevice...");

			return this.mBluetoothAdapter.getBondedDevices();
		}

		public boolean BTO_ConnectDevice(String strMac) {
			Log.d("REGOLIB", "BTO_ConnectDevice...");

			boolean bRet = false;
			try {
				this.mmDevice = this.mBluetoothAdapter.getRemoteDevice(strMac);

				this.mmSocket = this.mmDevice
						.createRfcommSocketToServiceRecord(MobilePrinter.MY_UUID);
			}
			catch (IOException e) {
				Log.d("REGOLIB",
						"BTO_ConnectDevice --> create " + e.getMessage());
				return bRet;
			}

			this.mBluetoothAdapter.cancelDiscovery();
			try {
				this.mmSocket.connect();
				bRet = true;
			}
			catch (IOException e) {
				Log.d("REGOLIB",
						"BTO_ConnectDevice --> connect " + e.getMessage());
			}
			return bRet;
		}

		public boolean BTO_CloseDevice() {
			Log.d("REGOLIB", "BTO_CloseDevice...");

			boolean bRet = true;
			try {
				this.mmSocket.close();
			}
			catch (IOException e) {
				System.out.println("BTO_ConnectDevice close " + e.getMessage());
				bRet = false;
			}
			return bRet;
		}

		public boolean BTO_GetIOInterface() {
			Log.d("REGOLIB", "BTO_GetIOInterface...");
			try {
				this.mmInStream = this.mmSocket.getInputStream();
				this.mmOutStream = this.mmSocket.getOutputStream();
			}
			catch (IOException e) {
				Log.d("REGOLIB", "BTO_GetIOInterface " + e.getMessage());
				return false;
			}
			return true;
		}

		public boolean BTO_Write(byte[] buffer, int count) {
			Log.d("REGOLIB", "BTO_Write");
			try {
				this.mmOutStream.write(buffer, 0, count);
			}
			catch (IOException e) {
				Log.d("REGOLIB", "BTO_Write --> error " + e.getMessage());
				return false;
			}
			return true;
		}

		public boolean BTO_Read(byte[] buffer, int length) {
			Log.d("REGOLIB", "BTO_Read");
			try {
				this.mmInStream.read(buffer, 0, length);
			}
			catch (IOException e) {
				Log.d("REGOLIB", "BTO_Read --> error " + e.getMessage());
				return false;
			}
			return true;
		}
	}

	private class BTPrinter {
		public static final int BTP_reset = 0;
		public static final int BTP_feed = 1;
		public static final int BTP_left = 2;
		public static final int BTP_width = 3;
		public static final int BTP_opp = 4;
		public static final int BTP_linesp = 5;
		public static final int BTP_align = 6;
		public static final int BTP_dup = 7;
		public static final int BTP_barcode = 8;
		public static final int BTP_status = 9;
		public static final int BTP_barWidth = 10;
		public static final int BTP_barHeight = 11;
		public static final int BTP_barHRI = 12;
		public static final int BTP_format = 13;

		public BTPrinter() {
		}

		public int MakeFormat(boolean width, boolean height, boolean bold,
				boolean underline, boolean minifont) {
			byte[] com = new byte[1];
			com[0] = 0;
			if (width) {
				int tmp17_16 = 0;
				byte[] tmp17_14 = com;
				tmp17_14[tmp17_16] = ((byte) (tmp17_14[tmp17_16] | 0x20));
			}
			if (height) {
				int tmp31_30 = 0;
				byte[] tmp31_28 = com;
				tmp31_28[tmp31_30] = ((byte) (tmp31_28[tmp31_30] | 0x10));
			}
			if (bold) {
				int tmp45_44 = 0;
				byte[] tmp45_42 = com;
				tmp45_42[tmp45_44] = ((byte) (tmp45_42[tmp45_44] | 0x8));
			}
			if (underline) {
				int tmp60_59 = 0;
				byte[] tmp60_57 = com;
				tmp60_57[tmp60_59] = ((byte) (tmp60_57[tmp60_59] | 0x80));
			}
			if (minifont) {
				int tmp76_75 = 0;
				byte[] tmp76_73 = com;
				tmp76_73[tmp76_75] = ((byte) (tmp76_73[tmp76_75] | 0x1));
			}
			return com[0];
		}

		public byte[] MakeComm(int iType, int para1, int para2, byte[] iLength) {
			byte[] mBuff = new byte[20];
			int mLen = 0;

			switch (iType) {
			case 0:
				mBuff[0] = 27;
				mBuff[1] = 64;
				mLen = 2;
				break;
			case 1:
				mBuff[0] = 27;
				mBuff[1] = 74;
				mBuff[2] = ((byte) para1);
				mLen = 3;
				break;
			case 2:
				mBuff[0] = 29;
				mBuff[1] = 76;
				mBuff[2] = ((byte) (para1 / 256));
				mBuff[3] = ((byte) (para1 % 256));
				mLen = 4;
				break;
			case 3:
				mBuff[0] = 29;
				mBuff[1] = 87;
				mBuff[2] = ((byte) (para1 / 256));
				mBuff[3] = ((byte) (para1 % 256));
				mLen = 4;
				break;
			case 4:
				mBuff[0] = 29;
				mBuff[1] = 66;
				mBuff[2] = ((byte) para1);
				mLen = 3;
				break;
			case 5:
				if (1 == para2) {
					mBuff[0] = 27;
					mBuff[1] = 50;
					mLen = 2;
				}
				else {
					mBuff[0] = 27;
					mBuff[1] = 51;
					mBuff[2] = ((byte) para1);
					mLen = 3;
				}
				break;
			case 6:
				mBuff[0] = 27;
				mBuff[1] = 97;
				mBuff[2] = ((byte) para1);
				mLen = 3;
				break;
			case 7:
				mBuff[0] = 27;
				mBuff[1] = 71;
				mBuff[2] = ((byte) para1);
				mLen = 3;
				break;
			case 8:
				mBuff[0] = 29;
				mBuff[1] = 107;
				mBuff[2] = ((byte) para1);
				mBuff[3] = ((byte) para2);
				mLen = 4;
				break;
			case 10:
				mBuff[0] = 29;
				mBuff[1] = 119;
				mBuff[2] = ((byte) para1);
				mLen = 3;
				break;
			case 11:
				mBuff[0] = 29;
				mBuff[1] = 104;
				mBuff[2] = ((byte) para1);
				mLen = 3;
				break;
			case 12:
				mBuff[0] = 29;
				mBuff[1] = 72;
				mBuff[2] = ((byte) para1);
				mLen = 3;
				break;
			case 9:
				mBuff[0] = 27;
				mBuff[1] = 118;
				mLen = 2;
				break;
			case 13:
				mBuff[0] = 27;
				mBuff[1] = 33;
				mBuff[2] = ((byte) para1);
				mLen = 3;
				break;
			}

			iLength[0] = ((byte) mLen);
			return mBuff;
		}
	}

	public boolean printAndLineFeed() {
		Log.d("REGOLIB", "printAndLineFeed...");

		if (3 == this.iStatusCode) {
			byte[] data = new byte[1];
			data[0] = 0x0A;

			if (this.bto.BTO_Write(data, 1)) {
				return true;
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return false;
	}

	public boolean goBlackMark() {
		Log.d("REGOLIB", "goBlackMark...");

		if (3 == this.iStatusCode) {
			byte[] data = new byte[2];
			data[0] = 0x1D;
			data[1] = 0x0C;

			if (this.bto.BTO_Write(data, 2)) {
				return true;
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return false;
	}

	public boolean setPositionAbsolute(Integer pos) {
		Log.d("REGOLIB", "setPositionAbsolute...");

		if (3 == this.iStatusCode) {

			Integer valor = pos * 8;

			byte[] data = new byte[4];
			data[0] = 0x1B;
			data[1] = '$';
			data[2] = valor.byteValue();
			data[3] = 0;

			if (this.bto.BTO_Write(data, 4)) {
				return true;
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return false;
	}

	public boolean rotate80Graus() {
		// 3.2.7 - ESC V n
		Log.d("REGOLIB", "rotate80Graus...");

		if (3 == this.iStatusCode) {
			byte[] data = new byte[3];
			data[0] = 0x1B;
			data[1] = 'V';
			data[2] = 1;

			if (this.bto.BTO_Write(data, 3)) {
				return true;
			}
		}
		else {
			this.iPrintCode = 5;
		}
		return false;
	}

	public boolean sendDensity(int i) {
		Log.d("REGOLIB", "sendDensity...");

		if (3 == this.iStatusCode) {
			byte[] data = new byte[3];
			data[0] = 0x1D;
			data[1] = 0x45;
			data[2] = (byte) i;

			if (this.bto.BTO_Write(data, 3)) {
				return true;
			}
		}
		else {
			this.iPrintCode = 5;
		}

		return false;
	}

}